<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="反射的定义在运行状态中，对于任意一个类，都能够知道这个类的所有属性和方法；对于任意一个对象，都能够调用它的任意一个方法和属性；这种动态获取的信息以及动态调用对象的方法的功能称为反射。 在java中反射是基于class信息的  关于Class   Class是一个类，一个描述类的类（也就是描述类本身），封装了描述方法的Method，描述字段的Filed，描述构造器的Constructor等属性  对">
<meta property="og:type" content="article">
<meta property="og:title" content="kyssion-blog">
<meta property="og:url" content="http://yoursite.com/2018/10/09/java-注解反射动态代理-反射/index.html">
<meta property="og:site_name" content="kyssion-blog">
<meta property="og:description" content="反射的定义在运行状态中，对于任意一个类，都能够知道这个类的所有属性和方法；对于任意一个对象，都能够调用它的任意一个方法和属性；这种动态获取的信息以及动态调用对象的方法的功能称为反射。 在java中反射是基于class信息的  关于Class   Class是一个类，一个描述类的类（也就是描述类本身），封装了描述方法的Method，描述字段的Filed，描述构造器的Constructor等属性  对">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-10-09T07:22:37.337Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="kyssion-blog">
<meta name="twitter:description" content="反射的定义在运行状态中，对于任意一个类，都能够知道这个类的所有属性和方法；对于任意一个对象，都能够调用它的任意一个方法和属性；这种动态获取的信息以及动态调用对象的方法的功能称为反射。 在java中反射是基于class信息的  关于Class   Class是一个类，一个描述类的类（也就是描述类本身），封装了描述方法的Method，描述字段的Filed，描述构造器的Constructor等属性  对">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/10/09/java-注解反射动态代理-反射/"/>





  <title> | kyssion-blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">kyssion-blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br />
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />
            
            站点地图
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/java-注解反射动态代理-反射/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline"></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="反射的定义"><a href="#反射的定义" class="headerlink" title="反射的定义"></a>反射的定义</h2><p>在运行状态中，对于任意一个类，都能够知道这个类的所有属性和方法；对于任意一个对象，都能够调用它的任意一个方法和属性；这种动态获取的信息以及动态调用对象的方法的功能称为反射。</p>
<p>在java中反射是基于class信息的</p>
<blockquote>
<p>关于Class</p>
</blockquote>
<ol>
<li><p>Class是一个类，一个描述类的类（也就是描述类本身），封装了描述方法的Method，描述字段的Filed，描述构造器的Constructor等属性</p>
</li>
<li><p>对象照镜子后（反射）可以得到的信息：某个类的数据成员名、方法和构造器、某个类到底实现了哪些接口。</p>
</li>
<li><p>对于每个类而言，JRE 都为其保留一个不变的 Class 类型的对象。一个 Class 对象包含了特定某个类的有关信息。</p>
</li>
<li><p>Class 对象只能由系统建立对象</p>
</li>
<li><p>一个类在 JVM 中只会有一个Class实例</p>
</li>
</ol>
<h2 id="java-通过编程方法应用反射特性"><a href="#java-通过编程方法应用反射特性" class="headerlink" title="java 通过编程方法应用反射特性"></a>java 通过编程方法应用反射特性</h2><h3 id="1-获取类的方法"><a href="#1-获取类的方法" class="headerlink" title="1. 获取类的方法"></a>1. 获取类的方法</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//使用对象获取Class</span></span><br><span class="line">Class&lt;?&gt; itemClass = <span class="keyword">new</span> String().getClass();</span><br><span class="line"><span class="comment">//直接使用类名.class获取</span></span><br><span class="line">Class&lt;?&gt; itemClass2 = String.class;</span><br><span class="line"><span class="comment">//使用类的完整路径获取</span></span><br><span class="line">Class&lt;?&gt; itemClass3 = Class.forName(<span class="string">"java.lang.String"</span>);</span><br><span class="line"><span class="comment">//获取父类</span></span><br><span class="line">Class&lt;?&gt; fatherClass = itemClass.getSuperClass();</span><br></pre></td></tr></table></figure>
<h3 id="2-使用class类"><a href="#2-使用class类" class="headerlink" title="2. 使用class类"></a>2. 使用class类</h3><blockquote>
<p>使用newInstance生成对象</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">String string= (String) itemClass.newInstance();</span><br></pre></td></tr></table></figure>
<blockquote>
<p>使用构造方法生成对象</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Constructor&lt;?&gt; constructor = itemClass.getConstructor(Integer.class,String.class);</span><br><span class="line"><span class="comment">//返回这个class所含有的所有方法--不包括继承的方法--只能使用自己定义的构造方法</span></span><br><span class="line">Constructor&lt;?&gt;[] constructors = itemClass.getConstructors();</span><br><span class="line">constructor.newInstance(<span class="number">12</span>,<span class="string">"sf"</span>);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>其他用法</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//--返回成员枚举类型 如果不是枚举类型返回 null</span></span><br><span class="line">class1.getEnumConstants();</span><br><span class="line"><span class="comment">//---class 类强制转化方法</span></span><br><span class="line">class1.asSubclass(class2);<span class="comment">//将class1转化成class2</span></span><br><span class="line">class1.cast(i);<span class="comment">// 讲 i强制转化成 class1所表示的实现类</span></span><br><span class="line"><span class="comment">//获得classloader</span></span><br><span class="line">ClassLoader acClassLoader = class1.getClassLoader();</span><br><span class="line">Class&lt;?&gt;[] classes=class2.getInterfaces();<span class="comment">//获得接口数组</span></span><br><span class="line">String string = class1.getName();<span class="comment">//返回类名称</span></span><br><span class="line">Package string2 =class1.getPackage();<span class="comment">//返回变量的包类</span></span><br><span class="line">URL rUrl = class1.getResource(<span class="string">"xx"</span>);<span class="comment">//返回给定名称所属的资源</span></span><br><span class="line">InputStream string3 = class1.getResourceAsStream(<span class="string">"xxx"</span>);<span class="comment">//将给定的资源变成输入流</span></span><br></pre></td></tr></table></figure>
<h3 id="使用反射获取类加载器"><a href="#使用反射获取类加载器" class="headerlink" title="使用反射获取类加载器"></a>使用反射获取类加载器</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1、获取一个系统的类加载器</span></span><br><span class="line">ClassLoader classLoader = ClassLoader.getSystemClassLoader();</span><br><span class="line">System.out.println(<span class="string">"系统的类加载器--&gt;"</span> + classLoader);</span><br><span class="line"></span><br><span class="line"><span class="comment">//2、获取系统类加载器的父类加载器(扩展类加载器（extensions classLoader）)</span></span><br><span class="line">classLoader = classLoader.getParent();</span><br><span class="line">System.out.println(<span class="string">"扩展类加载器--&gt;"</span> + classLoader);</span><br><span class="line"></span><br><span class="line"><span class="comment">//3、获取扩展类加载器的父类加载器</span></span><br><span class="line"><span class="comment">//输出为Null,无法被Java程序直接引用</span></span><br><span class="line">classLoader = classLoader.getParent();</span><br><span class="line">System.out.println(<span class="string">"启动类加载器--&gt;"</span> + classLoader);</span><br><span class="line"></span><br><span class="line"><span class="comment">//4、测试当前类由哪个类加载器进行加载 ,结果就是系统的类加载器</span></span><br><span class="line">classLoader = Class.forName(<span class="string">"com.kys.test.InvokeTest"</span>).getClassLoader();</span><br><span class="line">System.out.println(<span class="string">"当前类由哪个类加载器进行加载--&gt;"</span>+classLoader);</span><br><span class="line"></span><br><span class="line"><span class="comment">//5、测试JDK提供的Object类由哪个类加载器负责加载的</span></span><br><span class="line">classLoader = Class.forName(<span class="string">"java.lang.Object"</span>).getClassLoader();</span><br><span class="line">System.out.println(<span class="string">"JDK提供的Object类由哪个类加载器加载--&gt;"</span> + classLoader);</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">#输出</span><br><span class="line">系统的类加载器--&gt;sun.misc.Launcher$AppClassLoader@<span class="number">18</span>b4aac2</span><br><span class="line">扩展类加载器--&gt;sun.misc.Launcher$ExtClassLoader@<span class="number">61</span>bbe9ba</span><br><span class="line">启动类加载器--&gt;<span class="keyword">null</span></span><br><span class="line">当前类由哪个类加载器进行加载--&gt;sun.misc.Launcher$AppClassLoader@<span class="number">18</span>b4aac2</span><br><span class="line">JDK提供的Object类由哪个类加载器加载--&gt;<span class="keyword">null</span></span><br></pre></td></tr></table></figure>
<h3 id="引申使用class-类获取资源"><a href="#引申使用class-类获取资源" class="headerlink" title="引申使用class 类获取资源"></a>引申使用class 类获取资源</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// /(左划线)表示使用classpath根目录进行下过关属性的查找</span></span><br><span class="line">TestRelativePath.class.getResource(“/test.txt”).getFile()</span><br><span class="line">Thread.currentThread().getContextClassLoader().getResource(“test.txt”).getFile()</span><br></pre></td></tr></table></figure>
<h3 id="method-获取类中的方法"><a href="#method-获取类中的方法" class="headerlink" title="method 获取类中的方法"></a>method 获取类中的方法</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//返回名称为xx参数为后面的变长数组的 直接成员方法</span></span><br><span class="line">Method method = class1.getDeclaredMethod(<span class="string">"xx"</span>, Integer.class);</span><br><span class="line"><span class="comment">//方法处理继承的方法</span></span><br><span class="line">Method method2 = class1.getMethod(<span class="string">"x"</span>, Integer.class);</span><br><span class="line">Annotation[][] annotations = method.getParameterAnnotations();<span class="comment">//返回一个注解的二维数组 --同上面</span></span><br><span class="line">Annotation[] annotations2=method.getAnnotations();</span><br><span class="line">Class&lt;?&gt;[] class3 = method.getParameterTypes();<span class="comment">//返回参数数组</span></span><br><span class="line">Class&lt;?&gt; class4 = method.getReturnType();<span class="comment">//返回返回值数组</span></span><br><span class="line"><span class="keyword">int</span> count=method.getParameterCount();<span class="comment">//返回参数的数量</span></span><br><span class="line">Parameter[] parameters=method.getParameters();<span class="comment">//返回参数列表</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">c</span> </span>= method.getDeclaringClass();<span class="comment">//返回这个方法对应的class类</span></span><br><span class="line">parameters[<span class="number">0</span>].getName();</span><br><span class="line">parameters[<span class="number">0</span>].getType();<span class="comment">//返回类型的class数组</span></span><br><span class="line"><span class="comment">//参数类型没有注入 参数</span></span><br><span class="line">method.invoke(<span class="keyword">new</span> ThisisCeshe(), <span class="keyword">null</span>);<span class="comment">//类似 FIeld的get方法--调用指定的实例的一个此method指定的方法</span></span><br></pre></td></tr></table></figure>
<h3 id="Field字段"><a href="#Field字段" class="headerlink" title="Field字段"></a>Field字段</h3><p>field 相关方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//--成员变量</span></span><br><span class="line">Field field =class1.getDeclaredField(<span class="string">"xixi"</span>);<span class="comment">//返回变量名称为 field 的直接成员变量名称</span></span><br><span class="line">Field field2= class1.getField(<span class="string">"xxixi"</span>);<span class="comment">//返回变量名称包括父元素的成员变量</span></span><br><span class="line"><span class="comment">//f ye e d</span></span><br><span class="line">Field[] fields=class1.getDeclaredFields();<span class="comment">//返回所有直接的成员变量</span></span><br><span class="line">Field[] fields2 = class1.getFields();<span class="comment">//返回所有的成员变量 包括父类进行继承的变量</span></span><br><span class="line">field.get(<span class="keyword">new</span> ThisisCeshe());<span class="comment">//返回object 传入一个得出field对象的class的实例才能对其使用 取出实例中的对应的field对象 --存在基本类型的那一坨方法</span></span><br><span class="line">field.getType();<span class="comment">//返回一个class 对象表明这个字段的类型</span></span><br><span class="line">field.getName();<span class="comment">//返回这个字段的名字</span></span><br><span class="line">field.set(<span class="keyword">new</span> Integer(<span class="number">12</span>), <span class="number">123</span>);<span class="comment">//在前一个对象中 加入 后一个变量到field指定的变量上 --存在基本类型的那一坨方法</span></span><br><span class="line"><span class="keyword">int</span> a=field.getModifiers();<span class="comment">//返回参数类型--public或者其他</span></span><br></pre></td></tr></table></figure>
<h3 id="Annotated字段"><a href="#Annotated字段" class="headerlink" title="Annotated字段"></a>Annotated字段</h3><p>所有继承和实现了AnnotatedElement接口的类都具有返回对应的接口信息的方法，比如class method feild constructor</p>
<blockquote>
<p>注意下面的代码 *** 表示全部的反射都支持，特殊情况使用||分割</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Target Target = ***.getAnnotation(Target.class);<span class="comment">//返回当前类中是否存在传入的注解 有的时候将会进行返回相应的注解</span></span><br><span class="line">***.isAnnotationPresent(Ceshi2.class);<span class="comment">//判断是否存在一个注解</span></span><br><span class="line">Annotation[] annotations = ***.getAnnotations();<span class="comment">//返回类中所有的注解包括继承的</span></span><br><span class="line">Annotation[] annotations2= ***.getDeclaredAnnotations();<span class="comment">//f返回类中直接继承的元素</span></span><br></pre></td></tr></table></figure>
<p>注解的用法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">Annotation[][] annotation = constructor.getParameterAnnotations();</span><br><span class="line"><span class="comment">//所使用类型判断 强制转换</span></span><br><span class="line"><span class="keyword">if</span>(annotation[<span class="number">0</span>][<span class="number">0</span>] <span class="keyword">instanceof</span> Target)&#123;</span><br><span class="line">    Target targets= (Target) annotation[<span class="number">0</span>][<span class="number">0</span>];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//直接获取直接使用</span></span><br><span class="line">Target annotation = ***.getAnnotation(Target.class);</span><br></pre></td></tr></table></figure>
<h3 id="特殊的enclose-方法"><a href="#特殊的enclose-方法" class="headerlink" title="特殊的enclose* 方法"></a>特殊的enclose* 方法</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.kys.test;</span><br><span class="line"><span class="keyword">import</span> java.lang.annotation.Annotation;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.AnnotatedElement;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Constructor;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Outer</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Outer</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 构造方法中的匿名内部类</span></span><br><span class="line">        InnerClass innerClass = <span class="keyword">new</span> InnerClass() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                getEnclosing(<span class="keyword">this</span>.getClass());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        innerClass.fun();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 匿名内部类</span></span><br><span class="line">    <span class="keyword">static</span> InnerClass innerClass = <span class="keyword">new</span> InnerClass() &#123;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            getEnclosing(<span class="keyword">this</span>.getClass());</span><br><span class="line">            <span class="comment">/**</span></span><br><span class="line"><span class="comment">             * enclosingClass=class reflect.Outer</span></span><br><span class="line"><span class="comment">             * enclosingConstructor=null</span></span><br><span class="line"><span class="comment">             * enclosingMethod=null</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 方法中的匿名内部类</span></span><br><span class="line">        InnerClass innerClass = <span class="keyword">new</span> InnerClass() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                getEnclosing(<span class="keyword">this</span>.getClass());</span><br><span class="line">                <span class="comment">/**</span></span><br><span class="line"><span class="comment">                 * enclosingClass=class reflect.Outer</span></span><br><span class="line"><span class="comment">                 * enclosingConstructor=null</span></span><br><span class="line"><span class="comment">                 * enclosingMethod=public static void reflect.Outer.test()</span></span><br><span class="line"><span class="comment">                 */</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        innerClass.fun();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 内部类</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">InnerClass</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">InnerClass</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"------内部类------"</span>);</span><br><span class="line">        getEnclosing(InnerClass.class);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"------匿名内部类------"</span>);</span><br><span class="line">        innerClass.fun();</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"------方法中的匿名内部类------"</span>);</span><br><span class="line">        Outer.test();</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"------构造函数中的匿名内部类------"</span>);</span><br><span class="line">        <span class="keyword">new</span> Outer();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * getEnclosingClass:该类是在那个类中定义的， 比如直接定义的内部类或匿名内部类</span></span><br><span class="line"><span class="comment">     * getEnclosingConstructor：该类是在哪个构造函数中定义的，比如构造方法中定义的匿名内部类</span></span><br><span class="line"><span class="comment">     * getEnclosingMethod：该类是在哪个方法中定义的，比如方法中定义的匿名内部类</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> cls</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">getEnclosing</span><span class="params">(Class cls)</span> </span>&#123;</span><br><span class="line">        Class enclosingClass = cls.getEnclosingClass();</span><br><span class="line">        Constructor enclosingConstructor = cls.getEnclosingConstructor();</span><br><span class="line">        Method enclosingMethod = cls.getEnclosingMethod();</span><br><span class="line">        System.out.println(<span class="string">"enclosingClass="</span> + enclosingClass);</span><br><span class="line">        System.out.println(<span class="string">"enclosingConstructor="</span> + enclosingConstructor);</span><br><span class="line">        System.out.println(<span class="string">"enclosingMethod="</span> + enclosingMethod);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这个方法是针对内部类的，如果这个class是一个内部类通过这个方法可以得出这个内部类对应的声明位置</p>
<h3 id="array-范型"><a href="#array-范型" class="headerlink" title="array 范型"></a>array 范型</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//创建一个第二个参数是边长参数的组 第一个参数是指定类型的数组</span></span><br><span class="line">Array[][] arrays=(Array[][]) Array.newInstance(Integer.class, <span class="number">100</span>,<span class="number">100</span>);</span><br><span class="line">Array.get(arrays, <span class="number">0</span>);<span class="comment">//返回制定对象索引的值 注意只能返回一维 并且返回的对象是object类型---相关  函数还有getint等基本类型</span></span><br><span class="line">Array.set(arrays, <span class="number">0</span>, <span class="keyword">new</span> Integer(<span class="number">12</span>));<span class="comment">//将指定的类型传递到数组指定的位置中</span></span><br></pre></td></tr></table></figure>
<h3 id="AccessibleObject"><a href="#AccessibleObject" class="headerlink" title="AccessibleObject"></a>AccessibleObject</h3><blockquote>
<p>AccessibleObject是 Method Fielt Construction的一个父类-都实现了此类的方法,这个类主要是扩展了访问权限的控制</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">AccessibleObject accessibleObject = <span class="keyword">null</span>;</span><br><span class="line">accessibleObject.isAnnotationPresent(hehehe.class);<span class="comment">//判断这个类是否实现了hehehe这个接口</span></span><br><span class="line">accessibleObject.getAnnotation(hehehe.class);</span><br><span class="line">accessibleObject.getAnnotations();</span><br><span class="line"><span class="comment">//!!!---设置当前东西的访问性--- 设置为true的时候将可以实现对私有变量的  该便似有变量和成员的访问性</span></span><br><span class="line">accessibleObject.setAccessible(<span class="keyword">true</span>);</span><br></pre></td></tr></table></figure>
<p>尤其要注意这个地方setAccessible(true); 正是通过这个配置方法的访问权限</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/10/09/jdbc驱动使用详解（三）单机分库分表下的性能测试/" rel="next" title="">
                <i class="fa fa-chevron-left"></i> 
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/10/09/junit4使用整理（三）spring整合/" rel="prev" title="">
                 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">kyssion</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">133</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#反射的定义"><span class="nav-number">1.</span> <span class="nav-text">反射的定义</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#java-通过编程方法应用反射特性"><span class="nav-number">2.</span> <span class="nav-text">java 通过编程方法应用反射特性</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-获取类的方法"><span class="nav-number">2.1.</span> <span class="nav-text">1. 获取类的方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-使用class类"><span class="nav-number">2.2.</span> <span class="nav-text">2. 使用class类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用反射获取类加载器"><span class="nav-number">2.3.</span> <span class="nav-text">使用反射获取类加载器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#引申使用class-类获取资源"><span class="nav-number">2.4.</span> <span class="nav-text">引申使用class 类获取资源</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#method-获取类中的方法"><span class="nav-number">2.5.</span> <span class="nav-text">method 获取类中的方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Field字段"><span class="nav-number">2.6.</span> <span class="nav-text">Field字段</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Annotated字段"><span class="nav-number">2.7.</span> <span class="nav-text">Annotated字段</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#特殊的enclose-方法"><span class="nav-number">2.8.</span> <span class="nav-text">特殊的enclose* 方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#array-范型"><span class="nav-number">2.9.</span> <span class="nav-text">array 范型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#AccessibleObject"><span class="nav-number">2.10.</span> <span class="nav-text">AccessibleObject</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">kyssion</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
